package ph.com.gs3.chat.adapters;

import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.chat.R;
import ph.com.gs3.chat.managers.ImageManager;
import ph.com.gs3.chat.model.valueobject.business.UserStub;
import ph.com.gs3.chat.utilities.FLLogger;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class OnlineUserListItemViewAdapter extends BaseAdapter {

	public static final String TAG = OnlineUserListItemViewAdapter.class.getSimpleName();

	private Context context;
	private List<UserStub> listViewData;

	private ImageManager imageManager;

	private String imageServer;

	private class OnlineUserListItemViewHolder {

		TextView tvHeader;
		ImageView ivAvatar;

		public OnlineUserListItemViewHolder(View view) {
			tvHeader = (TextView) view.findViewById(R.id.UserInfo_tvHeader);
			ivAvatar = (ImageView) view.findViewById(R.id.UserInfo_ivAvatarImage);
		}

	}

	public OnlineUserListItemViewAdapter(Context context, List<UserStub> listViewData) {
		if (listViewData == null) {
			listViewData = new ArrayList<UserStub>();
		}
		initialize(context, listViewData);
	}

	public OnlineUserListItemViewAdapter(Context context) {
		initialize(context, new ArrayList<UserStub>());
	}

	private void initialize(Context context, List<UserStub> listViewData) {

		this.context = context;
		this.listViewData = listViewData;

		imageManager = ImageManager.getDefaultInstance(context.getApplicationContext());

	}

	public void removeItem() {

	}

	@Override
	public int getCount() {
		return listViewData.size();
	}

	@Override
	public Object getItem(int position) {
		return listViewData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return Long.parseLong(listViewData.get(position).getId());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		OnlineUserListItemViewHolder holder = null;

		if (rowView == null) {
			// create the view if it's not yet created
			LayoutInflater inflater = (LayoutInflater) context
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.view_user_info_strip_small, parent, false);
			holder = new OnlineUserListItemViewHolder(rowView);
			rowView.setTag(holder);
		} else {
			holder = (OnlineUserListItemViewHolder) rowView.getTag();
		}

		UserStub data = (UserStub) getItem(position);

		if (data == null) {
			FLLogger.d(TAG, "null data");
		}

		// map the user stub data to views
		holder.tvHeader.setText(data.getDisplayName());
		if (data.getImageURL() != null && !data.getImageURL().isEmpty()) {
			imageManager.requestUserImage(data.getId(), imageServer + data.getImageURL(),
			        holder.ivAvatar);
		}

		return rowView;
	}

	public String getImageServer() {
		return imageServer;
	}

	public void setImageServer(String imageServer) {
		this.imageServer = imageServer;
	}

}
